package java_learn.java_jinjie.ThreadSafe;

/**
 * 卖票案例
 *
 * @Author: 李浩然
 * @Date: 2020/12/16 3:09 下午
 */
public class RunnableImpl implements Runnable {
    //定义一个多个线程共享的资源
    private int ticket = 100;

    /**
     * 卖票
     */
    @Override
    public void run() {
        //判断票是否存在
        while (true) {
            if (ticket > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-->正在卖第" + ticket-- + "张票");
            }
        }
    }
}
